﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.kindergarten {
    import flash.events.*;
    import flash.display.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.views.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.kiosk.*;
    import net.wooga.pets.models.world.*;

    public class BabyCycleMediator extends RenderListMediator implements IMediator, ITicking {

		[Inject]
        public var view:BabyCycleView;
		[Inject]
        public var world:WorldModel;
		[Inject]
        public var language:LanguageModel;
		[Inject]
        public var energy:EnergyModel;
		[Inject]
        public var inventory:InventoryModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var kiosk:KioskModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var gameViewModel:GameViewModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
        private var _babyVO:BabyVO;
        private var _showingBabyInterfaceOf:String;

        private static const SCALEFACTOR:Number = 3;
        private static const SCALEFACTOR_BIG:Number = 4;

        override public function onRegister():void{
            super.onRegister();
            this.view.init();
            eventMap.mapListener(eventDispatcher, EventConstants.CLICK_ON_BABY, this.onBabyClick);
            eventMap.mapListener(eventDispatcher, EventConstants.BEFORE_SCALE_CHANGED, this.onRollOut);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_BABY_TREATMENT, this.onSetBaby);
            eventMap.mapListener(eventDispatcher, EventConstants.FINALIZE_BABY_TREATMENT, this.onfinalizeTreatment);
            this.view.cycleContainer.addEventListener(MouseEvent.ROLL_OUT, this.onRollOut);
            this.view.cycleContainer.bg.addEventListener(MouseEvent.CLICK, this.onRollOut);
            this.view.hugButton.addEventListener(MouseEvent.CLICK, this.onHug);
            this.view.boostButton.addEventListener(MouseEvent.CLICK, this.onBoost);
            this.view.getText = this.language.getTextByKey("global_button_get_it");
            this.view.giveText = this.language.getTextByKey("global_button_give");
            this.view.boostText = this.language.getTextByKey("global_boost");
            this.view.textAnimationText = this.language.getTextByKey("global_amount_boosted_time");
            this.view.boostHeadlineText = this.language.getTextByKey("kindergarten_wake_up_in");
            this.view.hugText = this.language.getTextByKey("global_button_hug");
            this.view.findFamilyText = this.language.getTextByKey("kindergarten_find_family");
            this.view.findText = this.language.getTextByKey("global_button_find");
            this.view.initBoostbubble();
        }
        private function onSetBaby(_arg1:InfoEvent):void{
            if (this._showingBabyInterfaceOf == _arg1.id){
                this._babyVO = (this.world.getActor(_arg1.id) as BabyVO);
                this.view.setHugBar(this.hugPercentage, true);
                this.updateView();
            };
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            if (this._babyVO.currentTreatment){
                this.updateTime(_arg2);
            } else {
                this.removeTicking();
            };
        }
        private function updateTime(_arg1:Number):void{
            if (this._babyVO.currentTreatment.finishedIn > 0){
                this.view.setTimeText(this._babyVO.currentTreatment.finishedIn);
            } else {
                this.view.boostBubble.visible = false;
                this.updateView();
            };
            this.view.setHugBar(this.hugPercentage);
        }
        private function get hugPercentage():int{
            return (Math.floor(((100 * this.hugCount) / GameConstants.DOCTOR_MEAL_COUNT)));
        }
        private function get hugCount():int{
            var _local1:int = (this._babyVO.blockedAt - this.tickModel.timestamp);
            var _local2:Number = Math.ceil((_local1 / this.sessionModel.doctorMealDuration));
            return (_local2);
        }
        private function addTicking():void{
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        private function removeTicking():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
        }
        private function onfinalizeTreatment(_arg1:InfoEvent):void{
            if (((this._babyVO) && ((_arg1.id == this._babyVO.id)))){
                this.view.boostBubble.visible = false;
            };
        }
        private function onRollOut(_arg1:Event):void{
            this.view.hideCycle();
            this.removeTicking();
            this._showingBabyInterfaceOf = null;
            if (this._babyVO){
                dispatch(new InfoEvent(EventConstants.SET_SPEECHBUBBLE_VISIBLE, this._babyVO.id, true));
                this._babyVO.checkSpeechbubble();
            };
        }
        private function onBabyClick(_arg1:InfoEvent):void{
            var _local2:Number;
            this._babyVO = (this.world.getActor(_arg1.id) as BabyVO);
            if (this._babyVO.animationState != GameConstants.ANIMATION_STATE_WALKING){
                this.view.cycleContainer.x = _arg1.info.x;
                this.view.cycleContainer.y = _arg1.info.y;
                _local2 = (SCALEFACTOR / this.gameViewModel.gameViewScale);
                if (this.gameViewModel.gameViewScale >= 4){
                    _local2 = (SCALEFACTOR_BIG / this.gameViewModel.gameViewScale);
                };
                this.view.cycleContainer.scaleX = (this.view.cycleContainer.scaleY = _local2);
                this.view.setHugBar(this.hugPercentage, false);
                this.updateView();
                this.addTicking();
                dispatch(new InfoEvent(EventConstants.SET_SPEECHBUBBLE_VISIBLE, this._babyVO.id, false));
            };
        }
        private function updateView(_arg1:Event=null):void{
            var _local5:TreatmentVO;
            var _local6:String;
            var _local7:String;
            var _local8:Boolean;
            var _local9:Function;
            var _local10:Function;
            var _local2:Array = this._babyVO.getKindergartenTreatments();
            this.view.addCircle((_local2.length + 1));
            var _local3:Boolean;
            var _local4:int;
            while (_local4 < _local2.length) {
                _local5 = _local2[_local4];
                _local6 = _local5.needsItem;
                _local7 = _local6;
                _local8 = (this.inventory.getItemNumberByKey(_local6) > 0);
                _local9 = null;
                _local10 = null;
                if (_local5.state == GameConstants.BABY_STATE_NEEDED){
                    if (_local5 == this._babyVO.currentTreatment){
                        _local7 = (_local7 + "_needed");
                        if (_local8){
                            _local9 = this.onItemtGiveClick;
                        } else {
                            _local10 = this.onItemtGetClick;
                        };
                    } else {
                        _local7 = "locked";
                    };
                };
                this.view.setTreatment(_local4, _local7, _local9, _local10);
                if (_local5.state == GameConstants.BABY_STATE_STARTED){
                    _local3 = true;
                };
                _local4++;
            };
            if (this._babyVO.hasTreatmentsFinished){
                this.view.showFindParents(this.onFindClick);
            };
            this.setBoostButton(_local3);
            this._showingBabyInterfaceOf = this._babyVO.id;
            this.view.showCycle();
        }
        protected function setBoostButton(_arg1:Boolean):void{
            if (_arg1){
                this.view.boostBubble.visible = true;
                this.view.arc.visible = false;
            } else {
                this.view.boostBubble.visible = false;
                this.view.arc.visible = true;
            };
        }
        private function onItemtGiveClick(_arg1:Event):void{
            var _local2:String;
            var _local3:Object;
            var _local4:UpdateTreatmentEvent;
            (_arg1.currentTarget as MovieClip).removeEventListener(MouseEvent.CLICK, this.onItemtGiveClick);
            if (((this._babyVO.currentTreatment) && ((this._babyVO.currentTreatment.state == GameConstants.BABY_STATE_NEEDED)))){
                _local2 = this._babyVO.currentTreatment.needsItem;
                _local3 = {};
                _local3.item_type = _local2;
                _local4 = new UpdateTreatmentEvent(EventConstants.UPDATE_BABY_TREATMENT, this._babyVO, WorldConstants.START_BABY_TREATMENT, this.onTreatmentStarted, _local3);
                dispatch(_local4);
                this.createRewardAnimation(_local2);
                dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_GLUGG_GLUGG));
            };
        }
        private function createRewardAnimation(_arg1:String):void{
            var _local2:Object = new Object();
            _local2[EventConstants.ITEM_TYPE] = _arg1;
            _local2[EventConstants.TARGET_ID] = this._babyVO.id;
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ANIMATION_INTO_HOSPITAL, null, _local2));
        }
        private function onItemtGetClick(_arg1:Event):void{
            (_arg1.currentTarget as MovieClip).removeEventListener(MouseEvent.CLICK, this.onItemtGetClick);
            var _local2:String = this.kiosk.getCategory(this._babyVO.currentTreatment.needsItem);
            var _local3 = "";
            switch (_local2){
                case "Toy":
                    _local3 = GameConstants.TOYS_TAB;
                    break;
                case "Coffee":
                    _local3 = GameConstants.COFFEE_TAB;
                    break;
                case "Item":
                    _local3 = GameConstants.FOOD_TAB;
                    break;
            };
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.KIOSK_POPUP, _local3));
            var _local4:String = this.world.getRoomIdsOfType(GameConstants.TYPE_KIOSK)[0];
            var _local5:IRoom = (this.world.getRoomById(_local4) as IRoom);
            var _local6:Slot = this.roomSlotsModel.getSlotById(_local5.slotId);
            dispatch(new InfoEvent(EventConstants.SHOW_ROOM, String(_local6.id)));
        }
        private function onFindClick(_arg1:Event):void{
            this.onRollOut(null);
            dispatch(new InfoEvent(EventConstants.UPDATE_AND_SHOW_POPUP, GameConstants.ADOPT_POPUP, {id:this._babyVO.id}));
        }
        private function onTreatmentStarted(_arg1:ResultVO):void{
            var _local2:Object;
            if (_arg1.success){
                _local2 = _arg1.data.start_baby_treatment;
                this._babyVO.updateCurrentTreatment(_local2.treatment);
                dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ICONS, this._babyVO.roomId, _local2.items));
                dispatch(new InfoEvent(EventConstants.SET_BABY_TREATMENT, this._babyVO.id));
            };
        }
        private function onBoost(_arg1:MouseEvent):void{
            if (!this._babyVO.currentTreatment){
                return;
            };
            var _local2:int = this.energy.pendingCallsToBackendCount;
            var _local3 = (this._babyVO.currentTreatment.finishedIn > (_local2 * this.energy.boostDuration));
            var _local4 = (this.energy.energy > 0);
            if (((_local4) && (_local3))){
                dispatch(new InfoEvent(EventConstants.BOOST_MACHINE, this._babyVO.currentTreatment.id));
                this.view.startBoostAnimations(this._babyVO.currentTreatment.finishedIn, (this._babyVO.currentTreatment.finishedIn - this.energy.boostDuration));
            } else {
                if (((!(_local4)) && (_local3))){
                    dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.OUT_OF_ENERGY_POPUP));
                };
            };
        }
        private function onHug(_arg1:MouseEvent):void{
            this._babyVO.speechBubbleData = null;
            this._babyVO.speechBubbleType = null;
            var _local2:InfoEvent = new InfoEvent(EventConstants.HUG, this._babyVO.id);
            dispatch(new InfoEvent(EventConstants.ADD_ACTION_TO_AVATAR, this._babyVO.roomId, _local2));
            dispatch(new InfoEvent(EventConstants.SET_ANIMATION_STATE, this._babyVO.id, GameConstants.ANIMATION_STATE_HAPPY));
        }

    }
}//package net.wooga.pets.views.kindergarten 
